//
//  NewCRMViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/23.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "NewCRMViewController.h"
#import "CRMListTableViewCell.h"
#import "NewCRMListModel.h"
#import "YYModel.h"
#import "CRMMenuTableViewCell.h"
#import "SelectRightTypeView.h"
#import "LetterView.h"
#import "CRMBuyerViewController.h"
#import "BuyerSeacherViewController.h"
#import "V4InputBuyerAndSeller.h"
#import "CRMMarketViewController.h"
#import "MarkerPlaceViewController.h"
#import "BuyerAndSellerDetailViewController.h"



@interface NewCRMViewController ()<SelectRightTypeViewDelegate,UISearchBarDelegate,LetterViewDelegate>

@property(nonatomic,strong)UITableView * zhltableView;

@property (nonatomic,strong)NewCRMListModel *  CrmModel;

@end

@implementation NewCRMViewController
{
    NSMutableArray *arrayTypeRight;
    UISearchBar * txtSearch;
    SelectRightTypeView *selctTypeView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.CrmModel = [[NewCRMListModel alloc]init];
    arrayTypeRight = [[NSMutableArray alloc]init];
    [arrayTypeRight addObject:@{@"title":@"买家录入"}];
    [arrayTypeRight addObject:@{@"title":@"卖家录入"}];
    [arrayTypeRight addObject:@{@"title":@"销售单录入"}];
    
    [self rightButton:@"" image:@"jia" sel:@selector(rightButtonClicked)];
    
    
    
    [self.zhltableView reloadData];
    
    LetterView * letterview = [[LetterView alloc]initWithFrame:CGRectMake(kScreenWidth - 5 - 20, 64+50+20, 20, kScreenHeight - (64+50+20) - 15)];
    letterview.delegate = self;
    [self.view addSubview:letterview];
    
    [self makeData];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(makeData) name: @"CRMList" object:nil];
    
}
//数据刷新
-(void)makeData{
    
    
    self.CrmModel = [[NewCRMListModel alloc]init];
    krequestParam
    [NetEngine createHttpAction:@"customer/customerlist" withCache:NO withParams:dictparam withMask:SVProgressHUDMaskTypeClear onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
            NSDictionary * dic = resData[@"data"];
            
            self.CrmModel = [NewCRMListModel yy_modelWithDictionary:dic];
            
            [self.zhltableView reloadData];
            
            
        }else{
            
            
            
            
            
            
        }
        
    } onError:^(NSError *error) {
        
    }];
    
    
    
    
}

static NSString * CRMListTableViewCellID  = @"CRMListTableViewCellID";
static NSString * CRMMenuTableViewCellID  = @"CRMMenuTableViewCellID";

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.separatorInset=UIEdgeInsetsMake(0,0, 0, 0);
        zhltableView.backgroundColor = rgbGray;
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        zhltableView.tableHeaderView.frameHeight = 0;
        zhltableView.sectionFooterHeight = 0;
        [self.view addSubview:zhltableView];
        [zhltableView registerClass:[CRMListTableViewCell class] forCellReuseIdentifier:CRMListTableViewCellID];
        [zhltableView registerClass:[CRMMenuTableViewCell class] forCellReuseIdentifier:CRMMenuTableViewCellID];
        
        _zhltableView = zhltableView;
    }
    //    _zhltableView.tableHeaderView = self.tableivewHeaderView;
    
    
 
    
    return _zhltableView;
}
#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
   
    return 2 + self.CrmModel.namelist.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    switch (section) {
        case 0:
        {
            return 3;
        }
        break;
        case 1:
        {
         
            return self.CrmModel.keylist.count;
            
        }
        break;
        
        default:
        {
            DatalistModel * listmodel = self.CrmModel.datalist[section - 2];
        
            return listmodel.list.count;
        }
        break;
    }
    
    return 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    return 10+12+8+12+10;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if(section == 0){
        
        return 50;
        
    }
    
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if(section == 0){
        
        UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        headerview.backgroundColor = rgbGray;
        
        txtSearch= [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        txtSearch.delegate=self;
        txtSearch.placeholder=@"按关键字搜索";
        [headerview addSubview:txtSearch];
        [txtSearch setBackgroundImage:[[Utility Share] imageFromColor:rgbGray rect:txtSearch.frame.size]];
        
        
        return headerview;
        
    }else if (section == 1){
        
      
        UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
        headerview.backgroundColor = rgbGray;
        UIImageView * iconimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, (headerview.frameHeight - 15)/2, 15, 15)];
        iconimage.image = [UIImage imageNamed:@"xkf_25star"];
        [headerview addSubview:iconimage];
        UILabel * title = [RHMethods labelWithFrame:CGRectMake(XW(iconimage) + 10, 0, headerview.frameWidth, headerview.frameHeight) font:Font(15) color:rgbTitleDeepGray text:@"重要客户"];
        [headerview addSubview:title];
        return headerview;
        
        
    }else{
        
        
        NSString * name = self.CrmModel.namelist[section - 2];
        UIView * headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
        headerview.backgroundColor = rgbGray;
        UILabel * title = [RHMethods labelWithFrame:CGRectMake(10, 0, headerview.frameWidth, headerview.frameHeight) font:Font(15) color:rgbTitleDeepGray text:name];
        [headerview addSubview:title];
        return headerview;
        
        
    }
   
    
    
}
#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //菜单cell
    if(indexPath.section == 0){
        
        CRMMenuTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CRMMenuTableViewCellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.imageview.frame = CGRectMake(10, (cell.frameHeight - 24)/2, 24, 24);
        cell.imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"xkf_25icon0%ld",indexPath.row+1]];
        NSArray * titleArray = @[@"买家",@"卖家",@"销售单"];
        NSString * str = titleArray[indexPath.row];
        cell.titlelab.text = str;
        cell.titlelab.frame = CGRectMake(XW(cell.imageview) + 10, 0, [str widthWithFont:15], cell.frameHeight);
        CGFloat y = (cell.frameHeight - 15)/2;
        cell.redView.frame = CGRectMake(XW(cell.titlelab) + 3, y - 3, 5, 5);
        cell.redView.clipsToBounds = YES;
        cell.redView.layer.cornerRadius = cell.redView.frameHeight/2;
        cell.redView.backgroundColor = [UIColor redColor];
        cell.icon.frame = CGRectMake(kScreenWidth - 30 - 6, (cell.frameHeight - 29/2)/2, 8, 29/2);
        cell.icon.image = [UIImage imageNamed:@"xkf_searrighticon"];
        cell.redView.hidden = YES;
        if (indexPath.row == 0) {
            
            if ([self.CrmModel.b_red integerValue] > 0) {
                
                cell.redView.hidden = NO;
                
            }
            
            
            
        }else if (indexPath.row == 1){
            
            
            if ([self.CrmModel.s_red integerValue] > 0) {
                
                cell.redView.hidden = NO;
                
            }
            
            
        }
        
        return cell;
    }
    //人物相信cell
    CRMListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CRMListTableViewCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    //重要客户cell
    if(indexPath.section == 1){
        
        KeylistModel * model = self.CrmModel.keylist[indexPath.row];
        
        cell.ketmodel = model;
        
   //客户分类cell
    }else{
        
        ListModel * model = self.CrmModel.datalist[indexPath.section-2].list[indexPath.row];
        cell.model = model;
        
        
        
    }
    
    
    
    
    
    
    
    return cell;
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"%@",indexPath);
    
    
    EMWeakSelf(weakSelf);
    
    
    if (indexPath.section == 0) {
        
        switch (indexPath.row) {
            case 0:
            {
             
                NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                [dic setValue:@"1" forKey:@"type"];
                [dic setValue:^(){
                    
                    [weakSelf makeData];
                    
                } forKey:@"block"];
                [self pushController:[CRMBuyerViewController class] withInfo:dic withTitle:@"买家" withOther:nil];
            }
                break;
            case 1:
            {
                
                NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                [dic setValue:@"2" forKey:@"type"];
                [dic setValue:^(){
                    
                    [weakSelf makeData];
                    
                } forKey:@"block"];
                [self pushController:[CRMBuyerViewController class] withInfo:dic withTitle:@"卖家" withOther:nil];
            }
                break;
            case 2:
            {
                
                
                [self pushController:[CRMMarketViewController class] withInfo:nil withTitle:@"销售单" withOther:nil];
            }
                break;
                
                
            default:
                break;
        }
        
        
        
    }else if(indexPath.section == 1){
        
        
        KeylistModel * model = self.CrmModel.keylist[indexPath.row];
        
  
        NSMutableDictionary*mdic=[NSMutableDictionary new];
        [mdic setValue:model.z_id forKey:@"id"];
        
        [self pushController:[BuyerAndSellerDetailViewController class] withInfo:@"买家" withTitle:@"买家" withOther:mdic];
        
        
    }else{
        
        ListModel * model = self.CrmModel.datalist[indexPath.section-2].list[indexPath.row];
        NSMutableDictionary*mdic=[NSMutableDictionary new];
        [mdic setValue:model.z_id forKey:@"id"];
        
        [self pushController:[BuyerAndSellerDetailViewController class] withInfo:@"买家" withTitle:@"买家" withOther:mdic];
        
        
        
        
        
    }
    
    
    
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.view endEditing:YES];
    
    
}
-(void)rightButtonClicked{
    if (!selctTypeView) {
        selctTypeView=[[SelectRightTypeView alloc] init];
        selctTypeView.dicType=@{@"list":arrayTypeRight};
        selctTypeView.contentConter=YES;
    }
    selctTypeView.delegate=self;
    [selctTypeView show];
}
#pragma mark SelectRightTypeViewDelegate
-(void)selectRightTypeView:(SelectRightTypeView *)sview object:(NSDictionary *)dic index:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
        {
            [self pushController:[V4InputBuyerAndSeller class] withInfo:@"买家" withTitle:@"买家录入" withOther:nil];
        }
            break;
        case 1:
        {
            [self pushController:[V4InputBuyerAndSeller class] withInfo:@"卖家" withTitle:@"卖家录入" withOther:nil];
        }
            break;
        case 2:
        {
            [self pushController:[MarkerPlaceViewController class] withInfo:nil withTitle:@"销售单入录" withOther:nil];
        }
            break;
       
        default:
            break;
    }
    
    
    
    
    
}
#pragma mark LetterViewDelegate
-(void)letterViewDidTitle:(NSString *)title{
    
    NSArray * arr = [self.CrmModel.namelist mutableCopy];
    
    for (int i = 0; i < arr.count; i++) {
        
        if ([title isEqualToString:arr[i]]) {
            
            NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:i + 2];
            
            [self.zhltableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            
        }
        
        
        
        
    }

    
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    
    [self pushController:[BuyerSeacherViewController class] withOnlyInfo:nil];
    
    
    
    return NO;
}
@end
